import * as Cesium from 'cesium'
import {viewer} from './Cesium3d'
import {imageCanvasBase64} from './baseFun'
// 添加标识线
export const addIdentificationLine = (long, lat, height) => {
  viewer.entities.add({
    name: "",
    polyline: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights([
            long, lat, -100,
            long, lat, height
        ]),
        width: 1,
        // material: Cesium.Color.DODGERBLUE
        material : new Cesium.PolylineGlowMaterialProperty({ //发光线
            glowPower : 0.7,
            color : Cesium.Color.DODGERBLUE
        })
    }
  })
}

// 点绘制方法
// billboard ={
//    image: 0, // 必填
//    verticalOrigin: Cesium.VerticalOrigin.CENTER
// }
export const addMarker = (long, lat, height, billboard) => {
  let imgs = {
    0: "场景标签/创业创新中心.png",
    1: "场景标签/公共服务中心.png"
  }
  billboard.image = imgs[billboard.image] // 通过billboard.img获取imgs序号
  let default_billboard = {
    image: "场景标签/创业创新中心.png", 
    show: true, // default
    pixelOffset: new Cesium.Cartesian2(0, 0), // default: (0, 0)
    eyeOffset: new Cesium.Cartesian3(0.0, 0.0, 0.0), // default
    horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
    verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
    scale: 2.0, // default: 1.0
    // color: Cesium.Color.LIME, // default: WHITE
    // rotation: Cesium.Math.PI_OVER_FOUR, // default: 0.0
    alignedAxis: Cesium.Cartesian3.ZERO, // default
    width: 100, // default: undefined
    height: 25, // default: undefined
  }
  Object.assign(default_billboard, billboard)

  // 删除所有覆盖物
  // viewer.entities.removeAll()
  // 添加点
  viewer.entities.add({
      name: 'name',
      position: Cesium.Cartesian3.fromDegrees(long, lat, height),
      billboard: default_billboard
  })
  addIdentificationLine(long, lat, height)
}

// 多点绘制方法 dataArr-点信息集,billboard图标
export const addMarkers = (dataArr, billboard) => {
  viewer.entities.removeAll()
  let position_arr = [], markerObj

  imageCanvasBase64(billboard.image, (image) => {
    billboard.image = image

    dataArr.forEach((item, i) => {
      position_arr.push(Cesium.Cartesian3.fromDegrees(item.longitude, item.latitude, item.altitude))
      let options = {
        name: item.name,
        position: Cesium.Cartesian3.fromDegrees(item.longitude, item.latitude, item.altitude),
        billboard: billboard,
      }
      Object.assign(options, item)
      markerObj = viewer.entities.add(options)

      addIdentificationLine(item.longitude, item.latitude, item.altitude)

      item.entities_id = markerObj.id
    })

    access()
  })

  // console.log(position_arr)
  // 构建球并取中心点
  // let polyCenter = Cesium.BoundingSphere.gromPoints(position_arr).center
  // console.log(polyCenter)
  // 中心点贴地偏移， 移到海拔为0
  // polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter)
  // console.log(polyCenter)

  // this.flyToFun(-2450976.8164357054, 4571318.8577052, 3702844.70659926, true)
}
const access = () => {
  viewer.scene.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(121.60886,29.892052,500), // 世界坐标点
      orientation: {
        heading: 6.2495626988216,
        pitch: -0.7591124304684471,
        roll: 6.283037533269935
      }
  })
}

// 划线
export const polyGroundPrimitive = (line,width) => {
  const greenWall = viewer.entities.add({
    name: "Green wall from surface with outline",
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(line),
      width: width,
      // material: new Cesium.ImageMaterialProperty({image:'juxing.png', transparent:true}),
      material: new Cesium.Color(1, 0, 0, 0.5),

      clampToGround: true
      // outline: false,
    },
  })
  // viewer.zoomTo(viewer.entities)
}
// 发光圈
export const lightingWall = (line) =>{
  const greenWall = viewer.entities.add({
    name: "Green wall from surface with outline",
    wall: {
      positions: Cesium.Cartesian3.fromDegreesArrayHeights(line),
      // '@/assets/juxing.png'
      
      material: new Cesium.ImageMaterialProperty({image:'juxing.png', transparent:true}),
      outline: false,
    }
  })
  // viewer.zoomTo(viewer.entities)
}
//遮罩层
export const polyBox = () => {
  const redBox = viewer.entities.add({
    name: "Red box with black outline",
    position: Cesium.Cartesian3.fromDegrees(121.60798977734535, 29.8958578305504, 0.0),
    box: {
      dimensions: new Cesium.Cartesian3(100.0, 200.0, 80.0),
      material: Cesium.Color.RED.withAlpha(0.5),
      outline: false,
      // outlineColor: Cesium.Color.BLACK,
    },
  })
  viewer.zoomTo(viewer.entities)
}
//流向动态线
export const flowline = (line) =>{
  /*
	流动纹理线
	color 颜色
	duration 持续时间 毫秒
	*/
  class PolylineTrailLinkMaterialProperty {
    constructor(color, duration) {
      this._definitionChanged = new Cesium.Event()
      this._color = undefined
      this._colorSubscription = undefined
      this.color = color
      this.duration = duration
      this._time = (new Date()).getTime()
    }
    getType(time) {
      return 'PolylineTrailLink'
    }
    getValue(time, result) {
      if (!Cesium.defined(result)) {
        result = {}
      }
      result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color)
      result.image = Cesium.Material.PolylineTrailLinkImage
      result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration
      return result
    }
    equals(other) {
      return this === other ||
        (other instanceof PolylineTrailLinkMaterialProperty &&
          Property.equals(this._color, other._color))
    }
  }
  Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
    isConstant: {
      get: function() {
        return false;
      }
    },
    definitionChanged: {
      get: function() {
        return this._definitionChanged;
      }
    },
    color: Cesium.createPropertyDescriptor('color')
  });
	Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
	Cesium.Material.PolylineTrailLinkImage = "juxing.png";
	Cesium.Material.PolylineTrailLinkSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
	                                         {\n\
															czm_material material = czm_getDefaultMaterial(materialInput);\n\
															vec2 st = materialInput.st;\n\
															vec4 colorImage =texture2D(image, vec2(fract(st.s - time), st.t));\n\
															material.alpha = colorImage.a * color.a;\n\
															material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
															return material;\n\
														}";
  Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
		fabric: {
			type: Cesium.Material.PolylineTrailLinkType,
			uniforms: {
				color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
				image: Cesium.Material.PolylineTrailLinkImage,
				time: 0
			},
			source: Cesium.Material.PolylineTrailLinkSource
	  },
	  translucent: function(material) {
		  return true;
	  }
	});
  viewer.entities.add({
    name:'WallTrail',
    wall:{
      positions : Cesium.Cartesian3.fromDegreesArrayHeights(line),
      material: new PolylineTrailLinkMaterialProperty(Cesium.Color.WHITE, 9000)
    }
  })
}